import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class SolrDemo {

    /**
     * 建立索引
     */
    @Test
    public void solrIndex() {
        String url = "http://localhost:8020/solr/core0";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "1");
        doc.addField("name", "中国");
        doc.addField("content", "今天好冷是啊啊");
        doc.addField("testik", "今天好冷");//中文分词
        try {
            solrServer.add(doc);
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 建立索引2
     */
    @Test
    public void solrIndex2() {
        String url = "http://localhost:8020/solr/core0";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", UUID.randomUUID());
        doc.addField("eid", "3");
        doc.addField("fieldname", "a");//大字段类型：型号/规格
        doc.addField("bigfield", "你奶奶的疯狂拉升JFK拉近双方");//大字段
        doc.addField("productname", "JFK拉萨JFK拉萨解放；监控力度");//产品名称/注册证名称
        try {
            solrServer.add(doc);
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检索索引
     * 1、在程序中设定的默认搜索域优先级 > schema.xml 文件配置中的默认搜索域，关系为覆盖 不是叠加
     * 2、在搜索词前面加入搜索域优先级 > schemal.xml 文件配置的qf值 > schema.xml 文件配置中的默认搜索域
     * 3、在程序中用setFields函数可以知道显示域，并且该指定方法优先级 > schemaconfig.xml配置的fl值  为覆盖关系
     */
    @Test
    public void solrSearcher() throws SolrServerException {
        String url = "http://localhost:8020/solr";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        //查询条件
        SolrQuery params = new SolrQuery("bigfield:LPAR");
        //搜索词中加搜索域
        //SolrQuery params = new SolrQuery("name:今天好冷是啊啊");
        //或者关系
        //SolrQuery params = new SolrQuery("name:今天好冷是啊啊OR content:今天好冷");
        //SolrQuery params = new SolrQuery("name:今天好冷是啊啊AND content:xxx");
        //设置此条件为content 意义不大
        //params.setParam("df","content");

        //显示域
        String[] fields = {"id", "bigfield"};
        params.setFields(fields);
        //高亮
        params.addHighlightField("bigfield");//高亮值
        params.setHighlight(true);//高亮开关
        params.setHighlightSimplePre("<em class='highlight'>");//前置
        params.setHighlightSimplePost("</em>");//后置

        //查询
        QueryResponse response = solrServer.query(params);
        //高亮值获取
        Map<String, Map<String, List<String>>> map = response.getHighlighting();
        //返回SolrDocumentList  实际上是ArrayList子类
        SolrDocumentList list = response.getResults();
        System.out.println(list.getNumFound());
        for (SolrDocument solrDocument : list) {
            System.out.println(map.get
                    (solrDocument.getFieldValue("id"))
                    .get("bigfield").get(0));//取出高亮值<em class='highlight'>今天</em>好冷
            System.out.println(solrDocument.get("bigfield"));
        }
    }

    /**
     * 删除索引
     *
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void delSolrIndex() throws IOException, SolrServerException {
        String url = "http://localhost:8020/solr";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        solrServer.deleteById("c7fe83b4-dbaa-4203-950d-7cbb55ef9735");
        solrServer.commit();
    }

    @Test
    public void Test4() throws IOException {
        String text="BC0415C";
        StringReader sr=new StringReader(text);
        IKSegmenter ik=new IKSegmenter(sr, false);
        Lexeme lex=null;
        while((lex=ik.next())!=null){
            System.out.print(lex.getLexemeText()+"|");
        }

    }
}